package com.xy.kafka.controller;

import com.xy.kafka.service.KafkaProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/kafka")
public class KafkaController {

    private final KafkaProducerService producerService;

    @Autowired
    public KafkaController(KafkaProducerService producerService) {
        this.producerService = producerService;
    }

    /**
     * 发送消息到默认主题tc-topic
     * @param message 消息内容
     * @return 响应结果
     */
    @PostMapping("/send")
    public String sendMessage(@RequestParam String message) {
        producerService.sendMessage(message);
        return "消息发送成功: " + message;
    }

    /**
     * 发送消息到指定主题
     * @param topic 主题名称
     * @param message 消息内容
     * @return 响应结果
     */
    @PostMapping("/send/{topic}")
    public String sendMessageToTopic(@PathVariable String topic, @RequestParam String message) {
        producerService.sendMessage(topic, message);
        return "消息发送成功到主题 [" + topic + "]: " + message;
    }

    /**
     * 健康检查接口
     * @return 健康状态
     */
    @GetMapping("/health")
    public String healthCheck() {
        return "Kafka服务运行正常";
    }
}